package com.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.yygh.hosp.repository.DepartmentRepository;
import com.yygh.hosp.service.DepartmentService;
import com.yygh.model.hosp.Department;

import com.yygh.vo.hosp.DepartmentQueryVo;
import com.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;
    @Override
    public void save(Map<String, Object> map) {
        String smap = JSONObject.toJSONString(map);
        Department department = JSONObject.parseObject(smap, Department.class);

        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();

        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);

            //判断
            if(departmentExist!=null) {
                departmentExist.setUpdateTime(new Date());
                departmentExist.setIsDeleted(0);
                departmentRepository.save(departmentExist);
            } else {
                department.setCreateTime(new Date());
                department.setUpdateTime(new Date());
                department.setIsDeleted(0);
                departmentRepository.save(department);
            }
        }



    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 创建Pageable对象，设置当前页和每页记录数
        //0是第一页
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department,matcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department!=null){
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号，查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        //所有科室列表 departmentList
        List<Department> departmentList =departmentRepository.findAll(example);

        Map<String, List<Department>> departmentList1 = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        Set<Map.Entry<String, List<Department>>> entries = departmentList1.entrySet();
        for (Map.Entry<String, List<Department>> entry :entries){
            String bigcode = entry.getKey();
            List<Department> value = entry.getValue();

            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(value.get(0).getBigname());

            //小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department:value){
                DepartmentVo departmentVo1 = new DepartmentVo();
                String depcode = department.getDepcode();
                String depname = department.getDepname();

                departmentVo1.setDepcode(depcode);
                departmentVo1.setDepname(depname);
                children.add(departmentVo1);
            }
            departmentVo.setChildren(children);

            result.add(departmentVo);
        }


        return result;
    }

    public Department getNameByhcAnddc(String hoscode,String depcode){
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }

}
